//
//  MYGActivityView.swift
//  MYDoctor
//
//  Created by Mo on 2024/3/6.
//

import UIKit

class MYGActivityView: UIView ,UITableViewDelegate,UITableViewDataSource{

    
    var activityBtn:UIButton!
    var popularBtn:UIButton!
    var typeLineView:UIView!
    var bottomTable:UITableView!
    var activeArr:[MYGActivesModel] = []
    var flowersArr:[MYGActivesModel] = []

    var tapTopIndexCompletion:((CGFloat)->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
        getDatasource()
    }
    private func getDatasource(){
        MYGNetwork<MYGHomePageAPI, MYGPageModel<MYGActivesModel>>.request(target: .getFlowers(param: [
            "pageSize":"4",
            "page":"1",
            "startTime":""])) {[weak self] item in
                self?.flowersArr.append(contentsOf: item.list)
                
            } failed: { _, error in
                MMYGHUD.showToast(text: error)
            }
        MYGNetwork<MYGHomePageAPI, MYGPageModel<MYGActivesModel>>.request(target: .getActives(param: [
            "pageSize":"4",
            "page":"1",
            "startTime":""])) {[weak self] item in
                guard let `self` = self else {return}
                self.activeArr.append(contentsOf: item.list)
                if let c = tapTopIndexCompletion {
                    let result = 120.0 + (Double(self.activeArr.count) * 55.0)
                    c(result)
                }
                self.bottomTable.reloadData()
            } failed: { _, error in
                MMYGHUD.showToast(text: error)
            }
    }
    func initUI(){
        let typeImageV = UIImageView()
        typeImageV.image = UIImage(named: "MYG_home_type")
        self.addSubview(typeImageV)
        typeImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18)
            make.top.equalToSuperview().offset(15)
            make.width.equalTo(3)
            make.height.equalTo(15)
        }
        let typeNameLabel = UILabel()
        typeNameLabel.textColor = RGBA(r: 34, g: 34, b: 34, a: 1)
        typeNameLabel.font = UIFont.systemFont(ofSize: 21, weight: .medium)
        typeNameLabel.text = "活跃榜"
        self.addSubview(typeNameLabel)
        typeNameLabel.snp.makeConstraints { make in
            make.left.equalTo(typeImageV.snp.right).offset(10.5)
            make.centerY.equalTo(typeImageV)
            make.height.equalTo(29.5)
        }
        
        let allBtn = UIButton()
        allBtn.setImage(UIImage(named: "MYG_home_moreJ"), for: .normal)
        allBtn.setTitle("查看全部", for: .normal)
        allBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        allBtn.setTitleColor(RGBA(r: 96, g: 102, b: 114, a: 1), for: .normal)
        allBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 13)
        allBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 79, bottom: 0, right: 0)
        self.addSubview(allBtn)
        allBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14.5)
            make.centerY.equalTo(typeImageV)
            make.width.equalTo(85)
            make.height.equalTo(15)
        }
        
        let whiteV = UIView()
        whiteV.layer.masksToBounds = true
        whiteV.layer.cornerRadius = 10
        whiteV.backgroundColor = RGBA(r: 255, g: 255, b: 255, a: 0.59)
        self.addSubview(whiteV)
        whiteV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(typeNameLabel.snp.bottom).offset(17.5)
            make.bottom.equalToSuperview()
        }
        
        typeLineView = UIView()
        typeLineView.backgroundColor = RGBA(r: 252, g: 73, b: 73, a: 1)
        typeLineView.layer.masksToBounds = true
        typeLineView.layer.cornerRadius = 1.5
        whiteV.addSubview(typeLineView)
        typeLineView.snp.makeConstraints { make in
            make.width.equalTo(16)
            make.height.equalTo(3)
            make.centerX.equalToSuperview().offset(-85)
            make.top.equalToSuperview().offset(38.5)
        }
        
        activityBtn = UIButton()
        activityBtn.setTitle("活跃榜", for: .normal)
        activityBtn.setTitleColor(RGBA(r: 164, g: 164, b: 164, a: 1), for: .normal)
        activityBtn.setTitleColor(RGBA(r: 252, g: 73, b: 73, a: 1), for: .selected)
        activityBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .regular)
        activityBtn.addTarget(self, action: #selector(changeData(btn:)), for: .touchUpInside)
        activityBtn.tag = 111
        activityBtn.isSelected = true
        whiteV.addSubview(activityBtn)
        activityBtn.snp.makeConstraints { make in
            make.bottom.equalTo(typeLineView.snp.top).offset(-1)
            make.centerX.equalToSuperview().offset(-85)
            make.height.equalTo(25)
        }
        
        popularBtn = UIButton()
        popularBtn.setTitle("红人榜", for: .normal)
        popularBtn.setTitleColor(RGBA(r: 164, g: 164, b: 164, a: 1), for: .normal)
        popularBtn.setTitleColor(RGBA(r: 252, g: 73, b: 73, a: 1), for: .selected)
        popularBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        popularBtn.addTarget(self, action: #selector(changeData(btn:)), for: .touchUpInside)
        popularBtn.tag = 222
        whiteV.addSubview(popularBtn)
        popularBtn.snp.makeConstraints { make in
            make.bottom.equalTo(typeLineView.snp.top).offset(-1)
            make.centerX.equalToSuperview().offset(85)
            make.height.equalTo(25)
        }
        
        bottomTable = UITableView()
        bottomTable.delegate = self
        bottomTable.backgroundColor = .clear
        bottomTable.dataSource = self
        bottomTable.register(cellWithClass: MYGActTableViewCell.self)
        whiteV.addSubview(bottomTable)
        bottomTable.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(typeLineView.snp.bottom).offset(9)
        }
    }

    @objc func changeData(btn:UIButton) {
        let otherHeight = 120.0
        if btn.tag == 111{
            activityBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .regular)
            popularBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            activityBtn.isSelected = true
            popularBtn.isSelected = false
            typeLineView.snp.updateConstraints { make in
                make.centerX.equalToSuperview().offset(-85)
            }
            if let c = tapTopIndexCompletion {
                let result = otherHeight + (Double(self.activeArr.count) * 55.0)
                c(result)
            }
        }else{
            popularBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .regular)
            activityBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            activityBtn.isSelected = false
            popularBtn.isSelected = true
            typeLineView.snp.updateConstraints { make in
                make.centerX.equalToSuperview().offset(85)
            }
            if let c = tapTopIndexCompletion {
                let result = otherHeight + (Double(self.flowersArr.count) * 55.0)
                c(result)
            }
        }
        bottomTable.reloadData()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if activityBtn.isSelected == true{
            return activeArr.count
        }else{
            return flowersArr.count
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGActTableViewCell.self, for: indexPath)
        cell.selectionStyle = .none
        if indexPath.row == 0{
            cell.numImageV.image = UIImage(named: "home_active_numberone")
        }else if indexPath.row < 3{
            cell.numImageV.image = UIImage(named: "home_active_numbertwo")
        }else{
            cell.numImageV.image = nil
            cell.backgroundColor = .rgba(209, 209, 209, 1)
        }
        if activityBtn.isSelected == true{
            cell.dataModel = activeArr[indexPath.row]
        }else{
            cell.dataModel = flowersArr[indexPath.row]
        }
        cell.numberLabel.text = String(format: "%d", indexPath.row + 1)
        return cell
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
